<?php
/**
 * @author: wangxiwen
 * @date: 2021/3/4 10:32 上午
 */

namespace App\Http\Controllers;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Model\User;
use App\Http\Model\UserVerify;
use App\Http\Services\FinanceServices;
use Illuminate\Http\Request;
use App\Http\Services\ApiServices;
use App\Http\Services\UserFilterServices;
use App\Lib\Response;

class ApiController extends BaseController
{
    protected $apiServices;
    protected $financeServices;
    public function __construct()
    {
        $this->apiServices = new ApiServices();
        $this->financeServices = new FinanceServices();
    }

    /**
     * 增加账户钻石余额
     * @param Request $request
     */
    public function increase(Request $request)
    {
        $userId = $this->isPositiveInteger($request->input('user_id'));
        $type = $this->isPositiveInteger($request->input('type'));
        $amount = $this->isPositiveInteger($request->input('amount'));
        $description = $request->input('description', '');
        $res = $this->apiServices->increase($userId, $type, $amount, $description);
        if(!$res) {
            OutJson::outJsonResult(Hint::ERROR);
        }
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 减少账户钻石余额
     * @param Request $request
     */
    public function decrease(Request $request)
    {
        $userId = $this->isPositiveInteger($request->input('user_id'));
        $type = $this->isPositiveInteger($request->input('type'));
        $amount = $this->isNegativeInteger($request->input('amount'));
        $description = $request->input('description', '');
        //检查是否实名,是否未成年,-1.未找到\0.未成年\1.成年
        $status = (new UserVerify())->isAdult($userId);
        //未成年：土司提示-为呵护未成年人健康成长，未成年人不允许充值（未满18岁）
        if ($status == 0) {
            OutJson::outJsonResult(Hint::JUVENILES_NOT_REWARD);
        }
        if ($status == -1) {
            OutJson::outJsonResult(Hint::ROOM_GIFT__USER_NOT_VERIFY);
        }
        //判断用户是否在禁止消费名单 add by zxiaofee
        if((new UserFilterServices)->inConsumeUserBlacklist($userId)){
            OutJson::outJsonResult(Hint::ROOM_GIFT_USER_CONSUME_FORBID);
        }
        //添加消费限制
        if((new FinanceServices())->reachConsumeToplimit($userId)){
            OutJson::outJsonResult(Hint::FINANCE_CONSUME_TOPLIMIT);
        }

        $res = $this->apiServices->decrease($userId, $type, $amount, $description);
        if(!$res) {
            OutJson::outJsonResult(Hint::ERROR);
        }
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 更新礼物背包
     * @param Request $request
     */
    public function sendGift(Request $request)
    {
        $userId = $this->isPositiveInteger($request->input('user_id'));
        $giftId = $this->isPositiveInteger($request->input('gift_id'));
        $giftNum = $this->isPositiveInteger($request->input('gift_number'));
        $type = $this->isPositiveInteger($request->input('type'));
        $res = $this->apiServices->sendGift($userId , $giftId, $giftNum, $type);
        if(!$res) {
            OutJson::outJsonResult(Hint::ERROR);
        }
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 直播房间
     * @param Request $request
     */
    public function liveRooms(Request $request)
    {
        $members = $request->input('members');
        if($members === null || $members === '') {
            OutJson::outJsonResult(Hint::ERROR);
        }
        $members = explode(',', $members);
        $data = $this->apiServices->liveRooms($members);
        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }

    public function checkUserStatus($userId){
        if((new User())->is_forbid($userId)) {
            return false;
        }
        if ((new User())->getAccountCancel($userId)){
            return false;
        }
        return true;
    }

    /**
     * 关注列表
     * @param Request $request
     */
    public function followList(Request $request)
    {
        $userId = $this->isPositiveInteger($request->input('user_id'));
        $members = $request->input('members');
        if($members === null || $members === '') {
            OutJson::outJsonResult(Hint::ERROR);
        }
        $members = explode(',', $members);
        $data = $this->apiServices->followList($userId, $members);
        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }

    /**
     * 添加关注
     * @param Request $request
     */
    public function addFollow(Request $request)
    {
        $userId = $this->isPositiveInteger($request->input('user_id'));
        $followUserId = $this->isPositiveInteger($request->input('follow_user_id'));
        $res = $this->apiServices->addFollow($userId ,$followUserId);
        if(!$res) {
            OutJson::outJsonResult(Hint::ERROR);
        }
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    //正整数
    private function isPositiveInteger($param)
    {
        if($param === null || !is_numeric($param) || strpos($param, '.') !== false || $param <= 0) {
            OutJson::outJsonResult(Hint::ERROR, '参数错误');
        }
        return (int)$param;
    }

    //负整数
    private function isNegativeInteger($param)
    {
        if($param === null || !is_numeric($param) || strpos($param, '.') !== false || $param >= 0) {
            OutJson::outJsonResult(Hint::ERROR, '参数错误');
        }
        return (int)$param;
    }

    /**
     * 房间真爱榜
     */
    public function ture_love_rank(Request $request){
        $room_id = $request->input('room_id');
        if (empty($room_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $page = !empty((int)$request->input('page_index')) && $request->input('page_index') >= 0 ? (int)$request->input('page_index') : 0;
        $page_size = !empty((int)$request->input('page_size')) && $request->input('page_size') > 0 ? (int)$request->input('page_size') : 10;
        $result = $this->financeServices->getRoomTrueLoveRank($room_id,$page,$page_size);
        Response::returnResult(Hint::SUCCESS,$result);
    }

}
